package com.lechi.web.controller;

import com.lechi.web.common.annotation.Log;
import com.lechi.web.common.core.controller.BaseController;
import com.lechi.web.common.core.domain.AjaxResult;
import com.lechi.web.common.core.page.TableDataInfo;
import com.lechi.web.common.enums.BusinessType;
import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.common.utils.SecurityUtils;
import com.lechi.web.constants.Constants;
import com.lechi.web.service.ICmsMainService;
import com.lechi.web.system.domain.CmsMain;
import com.lechi.web.vo.CmsMainAddVo;
import com.lechi.web.vo.CmsMainUpdateVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 内容发布管理Controller
 *
 * @author yangguang
 * @date 2024-07-10
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/cms/article")
public class CmsMainController extends BaseController {

    private final ICmsMainService iCmsMainService;

    /**
     * 查询内容发布管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmsMain cmsMain) {
        startPage();
        List<CmsMain> list = iCmsMainService.selectCmsMainList(cmsMain);
        return getDataTable(list);
    }

    /**
     * 获取内容发布管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        final CmsMain cmsMain = iCmsMainService.selectCmsMainById(id);
        return AjaxResult.success(cmsMain);
    }

    /**
     * 新增新闻内容
     */
    @PostMapping("/add")
    @Log(title = "新增新闻内容", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody CmsMainAddVo cmsMainAddVo) {
        CmsMain cmsMain = new CmsMain();
        BeanUtils.copyProperties(cmsMainAddVo, cmsMain);
        cmsMain.setCreateBy(SecurityUtils.getUsername());
        cmsMain.setCreateTime(new Date());
        return toAjax(iCmsMainService.insertCmsMain(cmsMain));
    }

    /**
     * 新增内容发布管理
     */
    @PostMapping("/publish")
    @Log(title = "修改官网文章", businessType = BusinessType.UPDATE)
    public AjaxResult publish(@RequestBody CmsMainUpdateVo cmsMainUpdateVo) {
        final CmsMain cmsMain = iCmsMainService.selectCmsMainById(cmsMainUpdateVo.getId());
        if (Objects.equals(cmsMain.getState(), Constants.UN_PUBLISH)) {
            throw new ServiceException(ErrorCodeEnum.UNAPPROVED_ERROR.getValue(), ErrorCodeEnum.UNAPPROVED_ERROR.getKey());
        }
        cmsMain.setPublishDate(new Date());
        cmsMain.setId(cmsMainUpdateVo.getId());
        cmsMain.setState(Constants.PUBLISHED);
        iCmsMainService.updateCmsMain(cmsMain);
        return AjaxResult.success();
    }

    /**
     * 修改内容发布管理
     */
    @Log(title = "修改发布内容", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody CmsMainUpdateVo cmsMainUpdateVo) {
        CmsMain cmsMain = new CmsMain();
        cmsMain.setUpdateBy(SecurityUtils.getUsername());
        cmsMain.setUpdateTime(new Date());
        BeanUtils.copyProperties(cmsMainUpdateVo, cmsMain);
        iCmsMainService.updateCmsMain(cmsMain);
        return AjaxResult.success();
    }

    /**
     * 删除内容发布管理
     */
    @Log(title = "内容发布管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        iCmsMainService.deleteCmsMainByIds(ids);
        return AjaxResult.success();
    }
}
